-- Fruit
-- Created by chengb Feb/3/2016
-- 果树格子

-- 初始化
function initFruit(self)
    -- 查看果树
    local function viewFruit()
        local pos = self.gridData:getPos();
        if DungeonInstanceM.isInInstance() then
            -- 如果在副本中，走副本奖励流程
            DungeonActionM.go("take_instance_bonus", pos);
        else
            DungeonActionM.go("take_fruit", pos);
        end
    end

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            if DungeonInstanceM.isInInstance() and
                not DungeonM.isAllMonsterDead() then
                -- 如果在副本中，需要击杀所有怪物方可拾取
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end

            -- 显示建筑查看界面
            self:openBuildingView(viewFruit);
        end
    end

    -- 果树图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 是否闪光
    if buildingInfo["shinning"] == 1 then
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end
end

-- 采摘果树的事件回调
function onTakeFruit(self, bonusInfo)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    -- 如果配置了奖励提示，提示一下
    if buildingInfo and buildingInfo["bonus_tip"] then
        alert(buildingInfo["bonus_tip"]);
    end

    -- 播放建筑奖励特效
    buildingBonusEffect(self);

    self:playBonusListEffect({ bonusInfo.bonus });

    if bonusInfo["debuff"] then
        local function callback()
            -- 提示中毒
            alert(getLocStr("take_fruit_got_debuff"));
        end
        performWithDelay(self, callback, 0.5);
    end
end
